package com.huage.courseplatform.application.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huage.courseplatform.application.mapper.DictMapper;
import com.huage.courseplatform.application.mapper.PeriodStudentCommentMapper;
import com.huage.courseplatform.application.model.entity.PeriodStudentComment;
import com.huage.courseplatform.application.model.entity.SysDict;
import com.huage.courseplatform.application.model.vo.PeriodStudentCommentAndTeacherVO;
import com.huage.courseplatform.application.service.DictService;
import com.huage.courseplatform.application.service.PeriodStudentCommentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;

@Service
public class PeriodStudentCommentServiceImpl extends ServiceImpl<PeriodStudentCommentMapper, PeriodStudentComment> implements PeriodStudentCommentService {

    @Resource
    PeriodStudentCommentMapper periodStudentCommentMapper;

    @Override
    @Cacheable(cacheNames="Service:PeriodStudentCommentService:id",key = "#id",unless="#result == null")
    public PeriodStudentCommentAndTeacherVO getCommentAndTeacherById(String id) {
        String userId = (String) StpUtil.getLoginId();
        return periodStudentCommentMapper.getCommentAndTeacherById(id,userId);
    }

    @Override
    //@Cacheable(cacheNames="Service:PeriodStudentCommentService:page",key = "'page:'+#page.pages+'-'+#page.size+'qw:'+#queryWrapper.expression",unless="#result == null")
    public Page<PeriodStudentCommentAndTeacherVO> getCommentAndTeacherPage(Page<PeriodStudentCommentAndTeacherVO> page, QueryWrapper<PeriodStudentCommentAndTeacherVO> qw) {
        String userId = (String) StpUtil.getLoginId();
        return periodStudentCommentMapper.getCommentAndTeacherPage(page,qw,userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Service:PeriodStudentCommentService:id", key = "#commentId")
    public void like(String commentId) {
        String userId = (String) StpUtil.getLoginId();
        PeriodStudentCommentAndTeacherVO periodStudentComment = periodStudentCommentMapper.getCommentAndTeacherById(commentId,userId);
        Assert.isTrue(!ObjectUtils.isEmpty(periodStudentComment),"找不到评论数据");
        if(periodStudentComment.getIsLike() == 1){
            update(new UpdateWrapper<PeriodStudentComment>()
                    .lambda()
                    .setSql("like_num = like_num-1")
                    .eq(PeriodStudentComment::getId,periodStudentComment.getId()));
            periodStudentCommentMapper.delLike(commentId);

        }else {
            update(new UpdateWrapper<PeriodStudentComment>()
                    .lambda()
                    .setSql("like_num = like_num+1")
                    .eq(PeriodStudentComment::getId,periodStudentComment.getId()));
            periodStudentCommentMapper.addLike(commentId,userId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        periodStudentCommentMapper.delLike((String) id);
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        idList.forEach(id->{
            periodStudentCommentMapper.delLike((String) id);
        });
        return super.removeByIds(idList);
    }
}
